package com.geek.javase.ch14.work.lockwork;

/*
 *@author wangpeng
 *2025/10/30 16:30
 *@description TODO
 */
public class TicketSeller implements Runnable{
    private int tickets = 100;
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "售出一张票，目前还剩" + (--tickets) + "张票");
                } else {
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        TicketSeller seller = new TicketSeller();
        Thread t1 = new Thread(seller, "A窗口");
        Thread t2 = new Thread(seller, "B窗口");
        Thread t3 = new Thread(seller, "C窗口");
        t1.start();
        t2.start();
        t3.start();
    }
}
